package com.hks360.third_party_library.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by xiaote on 2016/4/5.
 */
public class GsonService {

    public static <T> T parseJson(String jsonString, Class<T> clazz) {
        if (isEmptyJsonStr(jsonString)) {
            return null;
        }
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, clazz);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return t;
    }

    private static boolean isEmptyJsonStr(String jsonString) {
        if (jsonString == null) {
            return true;
        }
        switch (jsonString) {
            case "":
            case "null":
            case "{}":
                return true;
        }
        return false;
    }

    public static <T> List<T> parseJson(String jsonString, Type type) throws Exception{
        if (isEmptyJsonStr(jsonString)) {
            return null;
        }
        Gson gson = new Gson();
        List<T> list = gson.fromJson(jsonString, type);
        return list;
    }
}
